package com.joy.redisstudy;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;

import java.io.IOException;

public class RabbitMQConsumer {


    public static void main(String[] args) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();

        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPort(5672);
        Connection connection = connectionFactory.createConnection();
        Channel channel = connection.createChannel(true);

        try {
            channel.queueDeclare("TEST_MQ", false, false, false, null);
            channel.basicConsume("TEST_MQ",true,new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("收到消息："+new String(body));
                }
            });
            channel.basicAck(1,false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
